package orz.itrip.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import orz.itrip.DtoUtils;
import orz.itrip.PageUtils;
import orz.itrip.biz.service.CommentService;
import orz.itrip.biz.vo.GetCommentVo;
import orz.itrip.dto.Dto;
import orz.itrip.mapper.*;
import orz.itrip.pojo.*;
import orz.itrip.sftp.SFTPUtils;
import orz.itrip.vo.CommentAvgScoreVo;
import orz.itrip.vo.CommentCountVo;
import orz.itrip.vo.CommentImgVo;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private LabelDicMapper labelDicMapper;
    @Resource
    private HotelMapper hotelMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private HotelOrderMapper hotelOrderMapper;
    @Resource
    private ImageMapper imageMapper;
    @Override
    public Dto add(CommentImgVo commentImgVo, String token) {
        JSONObject jsonObject = JSON.parseObject(stringRedisTemplate.opsForValue().get(token));
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentImgVo,comment);
        comment.setUserId(jsonObject.getLong("id"));
        comment.setCreatedBy(jsonObject.getLong("id"));
        comment.setCreationDate(new Date());
        comment.setModifiedBy(jsonObject.getLong("id"));
        comment.setModifyDate(new Date());
        HotelOrder hotelOrder = new HotelOrder();
        hotelOrder.setOrderStatus(4);
        hotelOrder.setId(comment.getOrderId());
        hotelOrderMapper.updateByPrimaryKeySelective(hotelOrder);
        int count = 1;
        if (commentMapper.insertSelective(comment)>0){
            for (Image image:commentImgVo.getItripImages()) {
                image.setTargetId(commentImgVo.getHotelId());
                image.setCreatedBy(jsonObject.getLong("id"));
                image.setCreationDate(new Date());
                image.setType("2");
                image.setPosition(count++);
                imageMapper.insert(image);
            }
            return DtoUtils.getSuccessDto("新增评论成功");
        }
        return DtoUtils.getFalseDto("新增评论失败","60001");
    }

    @Override
    public Dto gettraveltype() {
        LabelDic labelDic = new LabelDic();
        labelDic.setParentId(107L);
        List<LabelDic> labelDics =  labelDicMapper.select(labelDic);
        if (labelDics!=null){
            return DtoUtils.getSuccesWithData(labelDics,"获取旅游类型列表成功");
        }
        return DtoUtils.getFalseDto("获取旅游类型列表失败","60002");
    }

    @Override
    public Dto gethoteldesc(Long hotelId) {
        Hotel hotel = hotelMapper.selectByPrimaryKey(hotelId);
        if (hotel!=null){
            return DtoUtils.getSuccesWithData(hotel,null);
        }
        return DtoUtils.getFalseDto(null,"60003");
    }

    @Override
    public Dto getimg(Long hotelId) {
        List<Image>images = hotelMapper.selectImage(hotelId,0);
        if (images!=null){
            return DtoUtils.getSuccesWithData(images,"获取酒店图片成功");
        }
        return DtoUtils.getFalseDto("获取酒店图片失败","60004");
    }

    @Override
    public Dto getcommentlist(GetCommentVo getCommentVo) {
        Page page = PageHelper.startPage(getCommentVo.getPageNo(),getCommentVo.getPageSize());
        PageUtils<Comment> pageUtils = new PageUtils<>();
        pageUtils.setPageSize(getCommentVo.getPageSize());
        pageUtils.setBeginPos(page.getStartRow());
        pageUtils.setPageSize(getCommentVo.getPageSize());
        pageUtils.setPageCount(getCommentVo.getPageSize());
        Comment comment = new Comment();
        comment.setHotelId(getCommentVo.getHotelId());
        List<Comment> comments = commentMapper.select(comment);
        if (comments!=null){
            pageUtils.setRows(comments);
            return DtoUtils.getSuccesWithData(pageUtils,null);
        }
        return DtoUtils.getFalseDto(null,"60005");
    }

    @Override
    public Dto gethotelscore(Long hotelId) {
        CommentAvgScoreVo commentAvgScoreVo = commentMapper.selectAvgScore(hotelId);
        if (commentAvgScoreVo!=null){
            return DtoUtils.getSuccesWithData(commentAvgScoreVo,"获取评分成功");
        }
        return DtoUtils.getFalseDto("获取评分失败","60006");
    }

    @Override
    public Dto getcount(Long hotelId) {
        CommentCountVo commentCountVo = commentMapper.selectCountByComment(hotelId);
        if (commentCountVo!=null){
            return DtoUtils.getSuccesWithData(commentCountVo,"获取酒店各类评论数成功");
        }
        return DtoUtils.getFalseDto("获取酒店各类评论数失败","60007");
    }

    @Override
    public Dto upload(MultipartFile file) throws IOException {
        String picName = file.getOriginalFilename();
        String dst = "/data/itrip/comment/"+ picName;
        InputStream inputStream = file.getInputStream();
        try {
            SFTPUtils.upload("192.168.110.128","root","123456","22",inputStream,dst);
            return DtoUtils.getSuccesWithData("http://img.itrip.project.bdqn.cn/comment/"+picName,"文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.getFalseDto("文件上传失败","60008");
        }
    }
}
